package com.huluobo.animdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView iv = findViewById(R.id.main_iv);
        Button tran_btn = findViewById(R.id.translate_btn);
        Button rotate_btn = findViewById(R.id.rotate_btn);
        Button scale_btn = findViewById(R.id.scale_btn);
        Button alpha_btn = findViewById(R.id.alpha_btn);
        Button set_btn = findViewById(R.id.set_btn);
        Button progress_btn = findViewById(R.id.progress_btn);
        ImageView progressIv = findViewById(R.id.main_iv2);
        Button value_btn = findViewById(R.id.valueAnimator_btn);
        Button object_btn = findViewById(R.id.objectAnimator_btn);
        Button inter_btn = findViewById(R.id.interpolator_btn);

        Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
        Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        Animation animationSet = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        tran_btn.setOnClickListener(v -> {
            iv.startAnimation(animationTranslate);
        });

        rotate_btn.setOnClickListener(v -> {
            iv.startAnimation(animationRotate);
        });

        scale_btn.setOnClickListener(v -> {
            iv.startAnimation(animationScale);
        });

        alpha_btn.setOnClickListener(v -> {
            iv.startAnimation(animationAlpha);
        });

        set_btn.setOnClickListener(v -> {
            iv.startAnimation(animationSet);
        });

        AnimationDrawable animationDrawable = (AnimationDrawable) progressIv.getDrawable();
        progress_btn.setOnClickListener(v -> {
            animationDrawable.start();
        });


        //属性动画 value属性动画
        value_btn.setOnClickListener(v -> {
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0f);
            valueAnimator.setDuration(5000);
            valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
            valueAnimator.setRepeatCount(ValueAnimator.INFINITE);

            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    float currentValue = (float) animation.getAnimatedValue();
                    Log.i("TagA", "currentValue:" + currentValue);
//                    iv.setAlpha();
//                    iv.setScaleX(currentValue);
//                    iv.setScaleY(currentValue);
//                    iv.setRotation();
//                    iv.setTranslationX();
//                    iv.setTranslationY();
                }
            });
            valueAnimator.start();
        });

        //对象的属性动画
        object_btn.setOnClickListener(v -> {
//            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f, 1f, 0.5f);
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(iv, "ScaleX", 0, 3);
            objectAnimator.setDuration(5000);
            objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
            objectAnimator.setRepeatCount(-1);
            objectAnimator.start();
        });

        inter_btn.setOnClickListener(v -> {
            ObjectAnimator translationY = ObjectAnimator.ofFloat(iv, "translationY", 50, 700);
//            translationY.setInterpolator(new AccelerateInterpolator());//加速插值器,开始慢,中间开始加速
//            translationY.setInterpolator(new DecelerateInterpolator());//减速插值器,开始快,中间开始减速
//            translationY.setInterpolator(new AccelerateDecelerateInterpolator());//默认插值器,加速减速插值器
//            translationY.setInterpolator(new AnticipateInterpolator());//反向加速,先后退一段距离,然后加速
            translationY.setInterpolator(new BounceInterpolator());//弹球插值器
//            translationY.setInterpolator(new CycleInterpolator(2));//循环插值器,会在设定的距离内反复移动
//            translationY.setInterpolator(new LinearInterpolator());//线性插值器 匀速直线运动
//            translationY.setInterpolator(new OvershootInterpolator());//超越插值器,查过重点一段距离,再返回重点,与反向加速是对应的
            translationY.setDuration(4000);
            translationY.setRepeatCount(-1);
            translationY.setRepeatMode(ValueAnimator.RESTART);
            translationY.start();
        });
    }
}